'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/pages/files_mzgl/dy_djcx/kh/djcx 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			formdata
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		console.log("formdata===", formdata);

		//获取accessToken
		let urlA = "https://open.douyin.com/oauth/client_token/"
		const accessToken = await uniCloud.httpclient.request(urlA, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
			},
			data: {
				client_key: formdata.ma_app_id, // id
				client_secret: formdata.appsecret, // 密钥
				grant_type: "client_credential",
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})


		// 有效时间为 2 个小时
		let access_token = accessToken.data.data.access_token

		if (formdata.c == 1) {
			console.log("查询小程序下所有短剧信息======");
			const batch_query = {
				offset: formdata.offset,
				limit: formdata.limit
			}
			let urlT = "https://open.douyin.com/api/playlet/v2/album/fetch/"
			const A = await uniCloud.httpclient.request(urlT, {
				method: 'POST',
				headers: {
					"Content-Type": "application/json",
					"access-token": access_token,
				},
				data: {
					ma_app_id: formdata.ma_app_id,
					query_type: formdata.c,
					batch_query: batch_query,
				},
				dataType: 'text', // 指定返回值为json格式，自动进行parseK
			})

			console.log("A=====", A.data);
			
			let num = await vk.baseDao.update({
				dbName: "dy_djcx", // 表名
				whereJson: { // 条件
					_id: formdata._id,
				},
				dataJson: { // 需要修改的数据
					result: A.data
				}
			});
		}
		if (formdata.c == 2) {
			console.log("查询一个短剧所有版本信息======");
			const single_query = {
				offset: formdata.offset,
				limit: formdata.limit,
				album_id: formdata.album_id
			}
			let urlT = "https://open.douyin.com/api/playlet/v2/album/fetch/"
			const A = await uniCloud.httpclient.request(urlT, {
				method: 'POST',
				headers: {
					"Content-Type": "application/json",
					"access-token": access_token,
				},
				data: {
					ma_app_id: formdata.ma_app_id,
					query_type: formdata.c,
					single_query: single_query,
				},
				dataType: 'text', // 指定返回值为json格式，自动进行parseK
			})
			console.log("A=====", A.data);
			
			let num = await vk.baseDao.update({
				dbName: "dy_djcx", // 表名
				whereJson: { // 条件
					_id: formdata._id,
				},
				dataJson: { // 需要修改的数据
					result: A.data
				}
			});
		}
		if (formdata.c == 3) {
			console.log("查询一个短剧某个版本下所有剧集信息======", formdata.ma_app_id);
			const detail_query = {
				offset: formdata.offset,
				limit: formdata.limit,
				album_id: formdata.album_id,
				version: formdata.version
			}
			let urlT = "https://open.douyin.com/api/playlet/v2/album/fetch/"
			const A = await uniCloud.httpclient.request(urlT, {
				method: 'POST',
				headers: {
					"Content-Type": "application/json",
					"access-token": access_token,
				},
				data: {

					ma_app_id: formdata.ma_app_id,
					query_type: formdata.c,
					detail_query: {
						offset: formdata.offset,
						limit: formdata.limit,
						album_id: formdata.album_id,
						version: formdata.version
					},
				},
				dataType: 'text', // 指定返回值为json格式，自动进行parseK
			})
			console.log("A=====", A.data);
			
			
			let num = await vk.baseDao.update({
				dbName: "dy_djcx", // 表名
				whereJson: { // 条件
					_id: formdata._id,
				},
				dataJson: { // 需要修改的数据
					result: A.data
				}
			});
		}




		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}